// Copyright (c) Omar AL Zabir. All rights reserved.
// For continued development and updates, visit http://msmvps.com/omar

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Linq;
using DataLayer;


/// <summary>
/// Summary description for WebServiceBase
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebServiceBase : System.Web.Services.WebService
{
   protected UserProfile Profile = HttpContext.Current.Profile as UserProfile;
   protected Guid UserKey = Functions.GetUserProviderKey();
   protected string UserName = "";
   protected LdapUser UserInfo;
   protected Guid[] UserRoleIDs = null;
   protected string strUserRoleID = "";
   public WebServiceBase()
    {
       if (!ActionValidator.ChkValid(ActionValidator.ActionTypeEnum.FailAction))
          throw new ApplicationException("Hacking Attempt Detected");

       UserName = Profile.UserName;
       UserInfo = new LdapUser(UserName);
       using (var db = DatabaseHelper.GetDashboardData())
       {
          /*
                      var widgets = from p in db.Widgets join w in db.WidgetPermissions
                                    where p.CategoryID == CategoryID && p.Enabled == true
                                    orderby p.OrderNo, p.Name
                                    select new { p.Name, p.Icon, p.ID };
           */
          UserRoleIDs = (from u in db.aspnet_UsersInRoles
                        where u.UserId == UserKey
                        select u.RoleId).ToArray();

          System.Text.StringBuilder UserRoles = new System.Text.StringBuilder();
          if (UserRoleIDs == null)
          {
             UserRoles.Append("'" + Guid.Empty.ToString() + "'");
          }
          else
          {             
             foreach (Guid id in UserRoleIDs)
             {
                UserRoles.Append("'" + id.ToString() + "',");
             }
             if (UserRoleIDs.Length > 0)
               UserRoles.Remove(UserRoles.Length - 1, 1);
          }
          strUserRoleID = UserRoles.ToString();
       }
    }
}
